package display;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.EventListener;

import javax.swing.JPanel;

import universe.Block;

public class MiniBlockDisplay extends JPanel{
	private Block nextBlock;
	private Container theContainer;	
	private boolean selected = false;
	private String blockType;
	private int offsetY = 0;
	private int borderX = 0;
	private int borderY = 0;
	
	public MiniBlockDisplay(Container pPanel, Block pBlock, String pBlockType){
		super();
		blockType = pBlockType;
		theContainer = pPanel;
		this.setBlock(pBlock);
		theContainer.add(this);
		borderX = display.Utils.bordureBlockSelected;
		borderY = display.Utils.bordureBlockSelected;
		setBounds();
	}
	
	public int getOffsetY() {
		return offsetY;
	}
	
	
	public void setColor(Color pColor){
		if(this.getBlockType().equals("A")){
			application.Utils.getCurrentTheme().setColorBlockA(pColor);
		}else if(this.getBlockType().equals("B")){
			application.Utils.getCurrentTheme().setColorBlockB(pColor);
		}else if(this.getBlockType().equals("C")){
			application.Utils.getCurrentTheme().setColorBlockC(pColor);
		}else if(this.getBlockType().equals("D")){
			application.Utils.getCurrentTheme().setColorBlockD(pColor);
		}else if(this.getBlockType().equals("E")){
			application.Utils.getCurrentTheme().setColorBlockE(pColor);
		}else if(this.getBlockType().equals("F")){
			application.Utils.getCurrentTheme().setColorBlockF(pColor);
		}else if(this.getBlockType().equals("G")){
			application.Utils.getCurrentTheme().setColorBlockG(pColor);
		}
	}
	
	
	
	
	public Color getColor(){
		Color returnedColor = Color.BLACK;

		if(this.getBlockType().equals("A")){
			returnedColor = application.Utils.getCurrentTheme().getColorBlockA();
		}else if(this.getBlockType().equals("B")){
			returnedColor = application.Utils.getCurrentTheme().getColorBlockB();
		}else if(this.getBlockType().equals("C")){
			returnedColor = application.Utils.getCurrentTheme().getColorBlockC();
		}else if(this.getBlockType().equals("D")){
			returnedColor = application.Utils.getCurrentTheme().getColorBlockD();
		}else if(this.getBlockType().equals("E")){
			returnedColor = application.Utils.getCurrentTheme().getColorBlockE();
		}else if(this.getBlockType().equals("F")){
			returnedColor = application.Utils.getCurrentTheme().getColorBlockF();
		}else if(this.getBlockType().equals("G")){
			returnedColor = application.Utils.getCurrentTheme().getColorBlockG();
		}
		
		return returnedColor;
	}
	
	
	private void setBounds(){
		this.setBounds(Utils.paddingMiniBlock, Utils.paddingMiniBlock+offsetY,display.Utils.getMiniBlockDisplayWidthPx + borderX * 2, display.Utils.getMiniBlockDisplayHeightPx + borderY * 2);
	}

	public void setOffsetY(int offsetY) {
		this.offsetY = offsetY;
		setBounds();
	}

	public String getBlockType() {
		return blockType;
	}

	public void setBlockType(String blockType) {
		this.blockType = blockType;
	}

	public void setBlock(Block pBlock){
		nextBlock = pBlock;
	}
	
	
	public void toggleSelected() {
		this.selected = !this.selected;
	}
	
	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public Container getContainer() {
		return theContainer;
	}

	public void paintComponent(Graphics g){
		int initialPointX = (display.Utils.getMiniBlockDisplayWidthPx /2) - ((nextBlock.getMinimalWidth() * display.Utils.sideWidthMini) /2);
		int initialPointY = (display.Utils.getMiniBlockDisplayHeightPx /2) - ((nextBlock.getMinimalHeight() * display.Utils.sideWidthMini) /2);
		int [][] minimalMatrix = nextBlock.getMinimalMatrix();	
		
		if(selected){

			if(borderX < 0 )borderX = 0;
			if(borderY < 0 )borderY = 0;
			g.setColor(Color.ORANGE);
			g.fillRect(0, 0,display.Utils.getMiniBlockDisplayWidthPx+(Utils.bordureBlockSelected*2), display.Utils.getMiniBlockDisplayHeightPx+(Utils.bordureBlockSelected*2));
		}
		
		g.setColor(Color.DARK_GRAY);
		g.fill3DRect(borderX, borderY,display.Utils.getMiniBlockDisplayWidthPx, display.Utils.getMiniBlockDisplayHeightPx,true);
		

		
		for(int i=0; i<nextBlock.getMinimalWidth();i++){
			for(int j=0; j<nextBlock.getMinimalHeight();j++){	
				if(minimalMatrix[i][j] != 0){
					g.setColor(Utils.getColor(minimalMatrix[i][j]));
					g.fill3DRect(borderX + initialPointX+(i*display.Utils.sideWidthMini), (borderY + initialPointY + (j*display.Utils.sideWidthMini)), Utils.sideWidthMini, Utils.sideWidthMini,true);
				}
			}
		}
		
		//g.setColor(Color.DARK_GRAY);
		//g.fillRect(0,0,1000,1000);
		
		
	}
}
